package mylibrary.jndi;

import java.io.File;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;

public class Example1 {

	public static void main(String[] args) {
		
		//String name = "d:/output.log";
		String name = "output.log";
			try {
				// Create a Properties object and set properties appropriately 
				Properties props = new Properties();
				props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
				props.put(Context.PROVIDER_URL, "file:///d:/");
				
				// Create the initial context from the properties we just created
				Context initialContext = new InitialContext(props);
				
				// Look up the object 
				Object obj = initialContext.lookup(name);
				//File file = (File) initialContext.lookup(name);
				if(name.equals("")) {
					System.out.println("Looked up the initial context");
				} else {
					System.out.println(name + " is bound to:" + obj);
				}
				

				initialContext.createSubcontext("sylilzy");		//create a subFolder under d:\
				initialContext.destroySubcontext("sylilzy");
				NamingEnumeration list = initialContext.list("/za");
				while(list.hasMore()) {
					NameClassPair nc = (NameClassPair) list.next();
					System.out.println(nc.getClassName());
					System.out.println(nc.getName());
					break;
					//System.out.println(nc);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
	}
}
